package com.chen.springboot.service.Impl;

import com.alibaba.excel.EasyExcel;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.chen.springboot.dto.RefundOrderDto;
import com.chen.springboot.entity.Item;
import com.chen.springboot.entity.RefundOrder;
import com.chen.springboot.exception.ServiceException;
import com.chen.springboot.mapper.RefundOrderMapper;
import com.chen.springboot.service.RefundOrderService;
import org.apache.logging.log4j.util.Strings;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Author Chen
 * @Date 2024/12/19 09:49
 */
@Service
public class RefundOrderServiceImpl implements RefundOrderService {

    @Autowired
    private RefundOrderMapper refundOrderMapper;

    @Override
    public Page<RefundOrder> getAll(Integer pageNum, Integer pageSize, String search) {
        // 新建分页插件
        Page<RefundOrder> page = new Page<>(pageNum, pageSize);
        // 新建模糊 wrapper
        LambdaQueryWrapper<RefundOrder> wrapper = Wrappers.lambdaQuery();
        // 排序
        wrapper.orderByDesc(RefundOrder::getId);
        if (!Strings.isEmpty(search))
            wrapper.like(RefundOrder::getUsername, search);
        return refundOrderMapper.selectPage(page, wrapper);
    }

    //post分页查询
    @Override
    public Page<RefundOrder>refundPage(Integer pageNum,Integer pageSize,String username,String orderNo){
        //新建分页插件
        Page<RefundOrder> page = new Page<>(pageNum, pageSize);
        //排序
//        LambdaQueryWrapper<RefundOrder> wrapper = Wrappers.lambdaQuery();
//        wrapper.orderByDesc(RefundOrder::getId);
        //查询条件
        LambdaQueryWrapper<RefundOrder> queryWrapper = new LambdaQueryWrapper<>();

        if(StringUtils.hasText(username))
            queryWrapper.like(RefundOrder::getUsername, username);

        if(StringUtils.hasText(orderNo))
            queryWrapper.like(RefundOrder::getOrderNo, orderNo);
        queryWrapper.orderByDesc(RefundOrder::getId);
        return refundOrderMapper.selectPage(page, queryWrapper);
    }


    @Override
    public void deleteOne(Integer id) {
        refundOrderMapper.deleteById(id);
    }

    @Override
    public void download(HttpServletResponse response) {
        Page<RefundOrder> result = getAll(1, Integer.MAX_VALUE, "");
        List<RefundOrder> refundOrders = result.getRecords();

        // 将 RefundOrder 对象列表转换为 RefundOrderDto 列表
        List<RefundOrderDto> refundOrderDtos = refundOrders.stream()
                .map(RefundOrderDto::new)
                .collect(Collectors.toList());

        //写出文件
        try {
            EasyExcel.write(response.getOutputStream(), RefundOrderDto.class)
                    .sheet()
                    .doWrite(refundOrderDtos);
        } catch (IOException e) {
            throw new ServiceException("401",e.getMessage());
        }
    }

    @Override
    public void refunded(RefundOrder refundOrder) {
        if (refundOrderMapper.updateRefundState(3,refundOrder.getOrderNo()) >= 0) {
            System.out.println("更新退款订单状态成功");
        } else {
            System.out.println("更新退款订单状态失败");
            throw new ServiceException("500", "请与管理员联系");
        }
        // 然后更新退款时间
        refundOrder.setRefundDate(new Date());
        // 然后在退款订单表里面保存退款订单
        refundOrderMapper.insert(refundOrder);
    }

    @Override
    public void refunding(RefundOrder refundOrder) {
        if (refundOrderMapper.updateRefundState(6,refundOrder.getOrderNo()) >= 0) {
            System.out.println("更新退款订单状态成功");
        } else {
            System.out.println("更新退款订单状态失败");
            throw new ServiceException("500", "请与管理员联系");
        }
        // 在退款订单表里面保存退款订单
        refundOrderMapper.insert(refundOrder);
    }

    @Override
    public void refundTime(String orderNo) {
        // 创建一个 UpdateWrapper 实例并设置更新条件
        UpdateWrapper<RefundOrder> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("order_no", orderNo); // 设置查询条件

        refundOrderMapper.updateRefundState(3,orderNo);
        // 构建要更新的数据
        RefundOrder refundOrder = new RefundOrder();
        refundOrder.setRefundDate(new Date()); // 设置为当前时间

        // 执行更新操作
        refundOrderMapper.update(refundOrder, updateWrapper);
    }
}
